package cn.com.egova.materialdesignexample.behavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by yuanchao on 2017/4/7.
 */

public class SnackBarBehavior extends CoordinatorLayout.Behavior<LinearLayout> {
    public SnackBarBehavior() {
    }

    public SnackBarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        Log.e("getTranslationY",dependency.getTranslationY()+"");
        Log.e("getHeight",dependency.getHeight()+"");
        Log.e("TranslationY-Height",dependency.getTranslationY()-dependency.getHeight()+"");
        child.setTranslationY(translationY);
        return true;
    }

    /**
     * float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()) 的理解:
     * 1.想象手机屏幕是一把带刻度的尺子, 从上到下依次是:0-----480
     * 2.dependency.getTranslationY() 是Snackbar在Y方向上平移到的位置,dependency.getHeight()是Snackbar在
     * Y方向上的初始位置.
     * 3.Snackbar出现的过程是:逐渐上移到最高点(跨度大约是Snackbar的高度), 然后下移到初始位置.
     * 4.综上dependency.getTranslationY() - dependency.getHeight()就是Snackbar在Y方向上滑动的距离(有方向),
     * 它的值的变化范围大约是0~ -107 ~0 ,那么 Math.min(0,dis)就好理解了.
     */
}
